package com.example.tencentdemo.utils;

public class Result<T> {
    private String code;
    private String msg;
    private T data;

    // 构造方法
    public Result(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    // 请求成功
    public static <T> Result<T> success(T data) {
        return new Result<>("200", "成功", data);
    }

    // 请求成功，无返回数据
    public static <T> Result<T> success() {
        return success(null);
    }

    // 请求失败
    public static <T> Result<T> fail(String code, String msg) {
        return new Result<>(code, msg, null);
    }

    // 请求失败，使用默认错误码
    public static <T> Result<T> fail(String msg) {
        return fail("400", msg);
    }

    // 未授权
    public static <T> Result<T> unauthorized(String msg) {
        return fail("401", msg);
    }

    // 禁止访问
    public static <T> Result<T> forbidden(String msg) {
        return fail("403", msg);
    }

    // 资源未找到
    public static <T> Result<T> notFound(String msg) {
        return fail("404", msg);
    }

    // 查询条件不存在
    public static <T> Result<T> conditionNotFound(String msg) {
        return fail("406", msg);
    }

    // 服务器内部错误
    public static <T> Result<T> internalServerError(String msg) {
        return fail("500", msg);
    }

    // Getters and setters
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
